/**
 * @Author: JiuQi 玖柒
 * @Date: 2021/7/8 22:21
 * @Description:
 */
public class T121 {
    public static void main(String[] args) {
        int[] prices = {7, 4, 3, 2, 8, 1};
        System.out.println(new Solution121().maxProfit(prices));
    }
}

//第一种方法

/*class Solution121 {
    public int maxProfit(int[] prices) {
        int max = 0;
        for (int i = 0; i < prices.length - 1; i++) {
            for (int j = i + 1; j < prices.length; j++) {
                int n = prices[j] - prices[i];
                max = max > n ? max : n;
            }
        }
        return max;
    }
}*/

class Solution121 {
    public int maxProfit(int[] prices) {
        int len = prices.length;
        int minPrice = prices[0];
        int max = 0;
        for (int i = 1; i < len; i++) {
            int profit = prices[i] - minPrice;
            //minPrice = Math.min(minPrice, prices[i]);
            //max = Math.max(profit, max);
            minPrice = minPrice < prices[i] ? minPrice : prices[i];
            max = max > profit ? max : profit;
        }
        return max;
    }
}
